<?php
namespace Api\Controller;

use Admin\Model\UserModel;
use Admin\Model\RoleModel;
use Common\Lib\Util\Validate;
use Admin\Model\AttachmentModel;

class PersonalController extends ApiController {
	
	private $userModel;
	private $roleModel;
	private $attachmentModel;

    function __construct() {
        parent::__construct();
        $this->userModel = new UserModel();
        $this->roleModel = new RoleModel();
        $this->attachmentModel = new AttachmentModel();
    }
    
    public function index(){
    	
    }
    
    //用户登录
    public function login($sendIdentify=''){
    	$user_name = I('user_name');
    	$user_pwd = I('user_pwd');
    	//用户类型，0 普通用户 1 扫楼用户
    	$type = I('type');
        $type = $type ? $type : 0;
    	
    	if (!$user_name || !$user_pwd) {
    		return $this->response('无效的用户名或密码',1);
    	}
    	
    	//是否需要验证码
    	$needIdentify = I('needIdentify');
    	if ($needIdentify == 1) {
	    	$identify = I('identify');
	    	if (!$identify || strcasecmp($identify,$sendIdentify) != 0) {
	    		return $this->response('登录失败，无效的验证码！',1);
    		}
    	}
    	
    	$userInfo = $this->userModel->getUserInfoByName($user_name,$user_pwd);
    	if ($userInfo) {
    		if ($userInfo['status'] == 1) {
    			//app端，验证该用户是否有权限登录
    			if (I('app') == 1) {
    				if ($userInfo['role']['app_login'] == 1) {
    					return $this->response('登录成功！',200,$userInfo);
    				};
    				return $this->response('登录失败，该用户无权限登录！',1);
    			}
    			return $this->response('登录成功！',200,$userInfo);
    		}
    		return $this->response('登录失败，该用户已被禁用！',1);
    	}
    	return $this->response('登录失败，无效的用户名或密码！',1);
    	
    }
    
    //用户注册
    public function register($sendEmsCode=''){
    	$user_name = I('user_name');
    	$mobile = I('mobile');
    	$ems_code = I('ems_code');
    	$user_pwd = I('user_pwd');
    	$user_pwd2 = I('user_pwd2');
    	$sendEmsCode = $sendEmsCode ? $sendEmsCode : I('send_ems_code');
    	if (!$user_name) {
    		return $this->response('用户名称不能为空！',1);
    	}
    	if ($this->userModel->checkUserExistByName($user_name) > 0) {
    		return $this->response('该用户名已存在！',1);
    	}
    	if (!$mobile) {
    		return $this->response('手机号码不能为空！',1);
    	}
    	if (!Validate::isMobile($mobile)) {
    		return $this->response('无效的手机号码！',1);
    	}
    	if ($this->userModel->checkUserExistByMobile($mobile) > 0) {
    		return $this->response('该手机号码已存在！',1);
    	}
    	if (!$ems_code || strcasecmp($ems_code,$sendEmsCode) != 0) {
    		return $this->response('无效的短信验证码！',1);
    	}
    	if (!$user_pwd) {
    		return $this->response('密码不能为空！',1);
    	}
    	if ($user_pwd != $user_pwd2) {
    		return $this->response('两次输入的密码不一致！',1);
    	}
    	$role = $this->roleModel->getRoleForRegister();
    	$data = array(
    		'user_name' => $user_name,
    		'password' => md5($user_pwd),
    		'mobile' => $mobile,
    		'create_time' => time(),
    		'status' => 1,
    		'role_id' => $role['id']
    	);
    	$rs = $this->userModel->add($data);
    	if ($rs !== false) {
    		$userInfo = $this->userModel->getUserInfoById($rs);
    		return $this->response('用户注册成功！',200, $userInfo);
    	}
    	return $this->response('用户注册失败！',1);
    }
    
    //获取当前登录用户的基本信息
    public function getUserInfo($user_id=0){
    	$user_id = $user_id ? $user_id : I('user_id');
    	if (!$user_id) {
    		return $this->response('无效的用户id！',1);
    	}
    	$userInfo = $this->userModel->getUserInfoById($user_id);
    	return $this->response('success',200,$userInfo);
    }
    
    //修改用户基本信息
    public function modifyUserInfo($user_id=0,$sendEmsCode=''){
    	$user_id = $user_id ? $user_id : I('user_id');
    	$sendEmsCode = $sendEmsCode ? $sendEmsCode : I('send_ems_code');
    	if (!$user_id) {
    		return $this->response('无效的用户id！',1);
    	}
    	$userInfo = $this->userModel->getUserInfoById($user_id);
    	$real_name = I('real_name');
    	$mobile = I('mobile');
    	$email = I('email');
    	$gender = I('gender');
    	$gender = $gender ? $gender : 0;
    	$data = array(
    		'real_name' => $real_name,
    		'gender' => $gender
    	);
    	if ($mobile) {
    		if (!Validate::isMobile($mobile)) {
    			return $this->response('无效的手机号码！',1);
	    	}
	    	//如果用户填写的手机号与原手机号不一致，需提供手机验证码（如果手机丢失，无法提供验证码，请走线下流程）
	    	if ($userInfo['mobile'] != $mobile) {
		    	$ems_code = I('ems_code');
		    	if (!$ems_code || strcasecmp($ems_code, $sendEmsCode) != 0) {
		    		return $this->response('无效的短信验证码！',1);
	    		}
	    	}
	    	$data['mobile'] = $mobile;
    	}
    	if ($email) {
    		if (!Validate::isEmail($email)) {
    			return $this->response('无效的邮箱地址！',1);
    		}
    		$data['email'] = $email;
    	}
    	$rs = $this->userModel->where(array('id'=>$user_id))->save($data);
    	if ($rs !== false) {
    		$attachementId = $_POST['attach_id'];
    		$avatar = '';
    		if ($attachementId) {
	    		$this->attachmentModel->deleteAttachementByType($user_id,'avatar');
	    		$this->attachmentModel->updateAttachement(array('rel_id'=>$user_id),$attachementId);
	    		//更新用户表中的头像信息
	    		$attache = $this->attachmentModel->getAttacheById($attachementId);
	    		$avatar = $attache['attachment'];
	    		$this->userModel->where(array('id'=>$user_id))->save(array('avatar'=>$avatar));
    		}
    		return $this->response('用户基本信息修改成功！',200,$avatar);
    	}
    	return $this->response('用户基本信息修改失败！',1);
    }
    
    //修改用户密码
    public function modifyPwd($user_id=0,$sendEmsCode=''){
    	$user_id = $user_id ? $user_id : I('user_id');
    	//用户提供的验证码
    	$ems_code = I('ems_code');
    	//系统提供的验证码
    	$sendEmsCode = $sendEmsCode ? $sendEmsCode : I('send_ems_code');
    	$pwd = I('pwd');
    	if (!$user_id) {
    		return $this->response('无效的用户id！',1);
    	}
    	if (!$ems_code || strcasecmp($ems_code, $sendEmsCode) != 0) {
    		return $this->response('无效的短信验证码！',1);
    	}
    	if (!$pwd) {
    		return $this->response('密码不能为空！',1);
    	}
    	
    	$userInfo = $this->userModel->getUserInfoById($user_id);
    	if (strcasecmp(md5($pwd), $userInfo['password']) == 0) {
    		return $this->response('新旧密码不能一致！',1);
    	}
    	
    	$data = array(
    		'password' => md5($pwd)
    	);
    	$rs = $this->userModel->where(array('id'=>$user_id))->save($data);
    	if ($rs !== false) {
    		return $this->response('用户密码修改成功！',200);
    	}
    	return $this->response('用户密码修改失败！',1);
    }
    
    //找回用户密码
    public function getBackPwd($sendEmsCode=''){
    	//手机号码
    	$mobile = I('mobile');
    	//用户提供的验证码
    	$ems_code = I('ems_code');
    	//系统提供的验证码
    	$sendEmsCode = $sendEmsCode ? $sendEmsCode : I('send_ems_code');
    	$pwd = I('pwd');
    	$pwd2 = I('pwd2');
    	if (!Validate::isMobile($mobile)) {
    		return $this->response('无效的手机号码！',1);
    	}
    	if (!$pwd) {
    		return $this->response('密码不能为空！',1);
    	}
//     	if ($pwd != $pwd2) {
//     		return $this->response('两次输入的密码不一致！',1);
//     	}
    	if (!$ems_code || strcasecmp($ems_code, $sendEmsCode) != 0) {
    		return $this->response('无效的短信验证码！',1);
    	}
    	$isExist = $this->userModel->checkUserExistByMobile($mobile);
    	if ($isExist == 0) {
    		return $this->response('该手机号码不存在！',1);
    	}
    	
    	$data = array(
    		'password' => md5($pwd)
    	);
    	$rs = $this->userModel->where(array('mobile'=>$mobile))->save($data);
    	if ($rs !== false) {
    		return $this->response('用户密码找回成功！',200);
    	}
    	return $this->response('用户密码找回失败！',1);
    }
    
}